<?php
 defined("BASEDIR") or die();
 
 class Application_Router_Standard_User
 {
	protected static $instance;
	protected $request, $userData;
	
	protected function __construct($request)
	{
		$this->request = $request;
		$this->getUser();
	}
	public function __get($var)
	{
		return $this->userData[$var];
	}
	public static function getInstance(Application_Router_Standard_Request $request)
	{
		if(!self::$instance instanceof self)
			self::$instance = new self($request);
		return self::$instance;
	}
	protected function getUser()
	{
		$db = Application_Model_DataBase::getInstance();
		$ses = Application_Model_SessionControl::getInstance();
		
		if($ses->un != "" ||  $this->request->username != "")
		{
			// Pobranie danych o użytkowniku
			$db->getTable("Pacjenci");
			$db->where("pesel='".($ses->un != "" ? $ses->un : $this->request->username)."'");
			$db->where("haslo='".($ses->pwd != "" ? $ses->pwd : md5($this->request->userpass))."'");
			$db->limit(1);
			
			// Pobranie aktualnego adresu ip
			$ip = (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
			
			$log = new Application_Model_Log();
			if($db->execute() == 1) {
			
				$level=1;
				($this->request->username == "" ? null : $log->addEntry("access", "Nastąpiło poprawne logowanie użytkownika ".$this->request->username." z adresu IP ".$ip));
				
				}
			else
			{
				// Pobranie danych o użytkowniku
				$db->getTable("Prowadzacy");
				$db->where("pid='".($ses->un != "" ? $ses->un : $this->request->username)."'");
				$db->where("haslo='".($ses->pwd != "" ? $ses->pwd : $this->request->userpass)."'");
				$db->limit(1);
				
				$log = new Application_Model_Log();
				if($db->execute() == 1) {
				$level=2;
					($this->request->username == "" ? null : $log->addEntry("access", "Nastąpiło poprawne logowanie prowadzącego ".$this->request->username." z adresu IP ".$ip));
					}
				else
				{
				($this->request->username == "" ? null : $log->addEntry("access", "Nastąpiła niepoprawna próba logowania użytkownika ".$this->request->username." z adresu IP ".$ip.", podane hasło: ".$this->request->userpass));
			
				
				
				}
			
			
			}
				$userData = $db->getResult();
		}
		
		if(!empty($userData))
		{
			$dfrom = mktime(0, 0, 0);	// Początek dnia
			$dto = mktime(23, 59, 59);	// Koniec dnia
			
			if($level==2&&$userData["typ"]=="admin")
				$level=10;
			
			// Zapisanie zmiennych sesyjnych
			$ses->un = $userData['pesel'];
			$ses->pwd = $userData['haslo'];
			$ses->logged_in = true;
			
			// Pobranie aktualnego adresu ip, w przypadku kiedy jest inny niż zapisany w sesji zakończenie sesji
			$ip = (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
			if(empty($ses->currentIP)) $ses->currentIP = $ip;
			else
				if($ses->currentIp != $ip) $ses->destroy();
			$userData["username"] = $userData["imie"]." ".$userData["nazwisko"];
			$userData["level"] =$level;
			$ses->level=$userData["level"];
			$ses->username = $userData["username"];
			$this->userData = $userData;
		}
	}
 }
?>